package com.llb666.e.shop.domain.service;

import com.github.pagehelper.PageHelper;
import com.llb666.e.shop.api.vo.ItemCommentVo;
import com.llb666.e.shop.api.vo.SearchItemsVo;
import com.llb666.e.shop.domain.entity.Items;
import com.llb666.e.shop.domain.entity.ItemsImg;
import com.llb666.e.shop.domain.entity.ItemsParam;
import com.llb666.e.shop.domain.entity.ItemsSpec;
import com.llb666.e.shop.domain.entity.bo.ItemCommentBo;
import com.llb666.e.shop.domain.entity.bo.ShopCartBo;
import com.llb666.e.shop.domain.repository.ItemsImgRepository;
import com.llb666.e.shop.domain.repository.ItemsParamRepository;
import com.llb666.e.shop.domain.repository.ItemsRepository;
import com.llb666.e.shop.domain.repository.ItemsSpecRepository;
import com.llb666.e.shop.infracore.utils.DesensitizationUtil;
import com.llb666.e.shop.infracore.web.PagedGridResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author llb666
 * @date 2022/9/12 22:09
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ItemService {

    private final ItemsImgRepository itemsImgRepository;
    private final ItemsSpecRepository itemsSpecRepository;
    private final ItemsRepository itemsRepository;
    private final ItemsParamRepository itemsParamRepository;

    /**
     * 查询商品图片信息.
     *
     * @param itemId 商品ID
     * @return 商品图片信息
     */
    public List<ItemsImg> queryItemsImgById(String itemId) {
        return itemsImgRepository.queryItemsImgById(itemId);
    }

    /**
     * 查询商品规格信息.
     *
     * @param itemId 商品ID
     * @return 商品规格信息
     */
    public List<ItemsSpec> queryItemsSpecById(String itemId) {
        return itemsSpecRepository.queryItemsSpecById(itemId);
    }

    /**
     * 查询商品详情.
     *
     * @param itemId 商品id
     * @return 商品基础信息
     */
    public Items queryById(String itemId) {
        return itemsRepository.queryById(itemId);
    }

    /**
     * 查询商品参数.
     *
     * @param itemId 商品Id
     * @return 商品参数
     */
    public ItemsParam queryItemsParamById(String itemId) {
        return itemsParamRepository.queryItemsParamById(itemId);
    }


    /**
     * 分页查询商品评论信息.
     *
     * @param itemId   商品id
     * @param level    商品等级
     * @param pageNum  当前页数
     * @param pageSize 每页显示条数
     * @return PagedGridResult
     */
    public PagedGridResult<ItemCommentBo> queryItemCommentByPage(String itemId, Integer level, Integer pageNum, Integer pageSize) {
        Map<String, Object> paramsMap = new HashMap<>(2);
        paramsMap.put("itemId", itemId);
        paramsMap.put("level", level);

        PagedGridResult<ItemCommentBo> page = itemsRepository.queryItemComments(paramsMap, pageNum, pageSize);
        List<ItemCommentBo> list = page.getRows();
        //脱敏
        list.forEach(e -> {
            e.setNickname(DesensitizationUtil.commonDisplay(e.getNickname()));
        });
        return page;
    }

    /**
     * 通过分类id搜索商品列表
     *
     * @param catId    类目id
     * @param sort     排序
     * @param page     当前页数
     * @param pageSize 每页显示条数
     * @return PagedGridResult
     */
    public PagedGridResult<SearchItemsVo> searchItems(Integer catId, String sort, Integer page, Integer pageSize) {
        Map<String, Object> paramsMap = new HashMap<>(2);
        paramsMap.put("catId", catId);
        paramsMap.put("sort", sort);
        return itemsRepository.searchItemsByThirdCat(paramsMap,page,pageSize);
    }

    /**
     * 通过规格id搜索商品列表.
     *
     * @param itemSpecIds 商品规格
     * @return List
     */
    public List<ShopCartBo> queryItemsBySpecIds(String itemSpecIds) {
        return itemsRepository.queryItemsBySpecIds(itemSpecIds);
    }

    /**
     * 根据商品id和评论等级查询商品评论数.
     *
     * @param id    商品id
     * @param level 评论等级
     * @return 商品评论
     */
    public Integer getCommentCount(String id, Integer level) {
        return itemsRepository.getCommentCount(id, level);
    }
}
